package com.wc.alorithm_luogu.P1217;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/4/20 11:53
 * @description https://www.luogu.com.cn/problem/P1217
 * 回文质数 Prime Palindromes
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();

        ArrayList<Integer> packet = new ArrayList<>();

        Main main = new Main();
        if (a%2==0){
            a++;
        }

        // 偶数不需要判断
        for (int i = a; i <= b; i+=2) {
            if (main.isPalindrome(i)&&main.isPrime(i)) {
                packet.add(i);
            }
        }

        for (int num : packet) {
            System.out.println(num);
        }
    }

    /**
     * 判断num是否为回文数
     *
     * @param num
     * @return
     */
    public boolean isPalindrome(int num) {
        int compareNum = num;
        int reverseNum = 0;

        while (num != 0) {
            reverseNum = reverseNum * 10 + num % 10;
            num /= 10;
        }
        return reverseNum == compareNum;
    }


    /**
     * 判断num是否为质数
     *
     * @param num
     * @return
     */
    public boolean isPrime(int num) {
        if (num == 1) {
            return false;
        }

        int mid = (int) Math.pow(num, 0.5);

        for (int i = 2; i <= mid; i++) {
            if (num % i == 0) {
                return false;
            }
        }

        return true;
    }
}
